import wepy from 'wepy'

export default class Home extends wepy.mixin {
    data = {
        value:'',
        searchList:[],
        // 搜索历史记录
        history:[]
    }

    config = {
    }

    methods = {
    //  搜索输入框发生变化
    onChang(e){
        this.value=e.detail.trim()
        clearTimeout(this.setId)
        if(this.value==="")return this.searchList=[]
        this.setId=setTimeout(async () => {
            //实现搜索的功能
            const {data:res}= await wepy.get('/goods/qsearch',{query:this.value})
            if(res.meta.status!=200) return wepy.baseToast()
            this.searchList=res.message
            this.$apply()
        }, 800);
    },
    // 手机按键盘搜索触发 模拟器：敲回车
    onSearch(){
        clearTimeout(this.setId)
        if(this.value==="")return this.searchList=[]
        if(!this.history.includes(this.value))
        {
            this.history.unshift(this.value)
            this.history=this.history.slice(0,10)
            wepy.setStorageSync('history',this.history)
        }
        wepy.navigateTo({
            url:`/pages/goods_list?query=${this.value}`
        })
    },
    onCancel(){
        this.searchList=[]
    },
    // 清空搜索历史
    removeHistory(){
        console.log(123);
        this.history=[]
        wepy.setStorageSync("history",[])
        
    },
    // 点击搜索标签跳转
    tagNavigate(val){
        wepy.navigateTo({
            url:`/pages/goods_list?query=${val}`
        })
    }
  }
  onLoad(){
      this.history=wepy.getStorageSync('history')||[]
  }
  onHide(){
      this.value=''
  }
}